#include "videodisplaysingle.h"
#include "ui_videodisplaysingle.h"

VideoDisplaySingle::VideoDisplaySingle(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VideoDisplaySingle)
{
    ui->setupUi(this);
    setVisible(true);
}

VideoDisplaySingle::~VideoDisplaySingle()
{
    qDebug() << "VideoDisplaySingle destruction!";
    delete ui;

    delete image_;
    delete image_layout_;
    delete image_widget_;
}

void VideoDisplaySingle::setSize(const qint32 width, const qint32 height)
{
    this->resize(width, height);
}

void VideoDisplaySingle::setWidget(qint32 image_x, qint32 image_y, qint32 image_width, qint32 image_height)
{
    if (image_widget_ == nullptr) {
        image_widget_ = new QWidget(this);
        image_layout_ = new QHBoxLayout(image_widget_);
        image_ = new QLabel();
        image_layout_->addWidget(image_);
        image_layout_->setSpacing(0);
        image_layout_->setContentsMargins(0, 0, 0, 0);
        image_widget_->setLayout(image_layout_);
        image_widget_->setGeometry(image_x, image_y, image_width, image_height);
        image1_width_ = image_width;
        image1_height_ = image_height;
    }
}

void VideoDisplaySingle::showImage(const QImage &image)
{
    if (image.height() > 0) {
        QPixmap pix = QPixmap::fromImage(image.scaled(image1_width_, image1_height_));
        image_->setPixmap(pix);
    }
}

void VideoDisplaySingle::resizeEvent(QResizeEvent *event)
{
    if (image_widget_ != nullptr) {
        image1_width_ = event->size().width();
        image1_height_ = event->size().height();
        image_widget_->setGeometry(0, 0, event->size().width(), event->size().height());
    }
}
